#include <iostream>

class Cat
{

public:
    std::string name;
    int age;
};

class Person
{

public:
    int age;
    Cat *cat;

    ~Person() { // 析构函数
        if (cat != nullptr) {
            delete cat;
            cat = nullptr;
        }
    }

};


int main(int argc, char const *argv[])
{
    Person* p1 = new Person();
    Cat* cat = new Cat();
    p1 -> cat = cat;
    std::cout << cat << std::endl;
    std::cout << p1 -> cat << std::endl;

    Person* p2 = new Person(*p1); // 浅拷贝

    std::cout << p2->cat << std::endl;

    delete p2; // 在浅拷贝的时候，将*cat给delete了，当 p1 再进行析构的时候，发现 *cat 已经是个野指针了，会报错误
                // 在cpp中，默认是浅拷贝

    std::cout << cat << std::endl;
    std::cout << p1 -> cat << std::endl;

    delete p1;

    std::cout << cat << std::endl;
    

    return 0;
}
